﻿using System;
using System.Linq.Expressions;
using Medora.DataContract;
using Medora.Model.Basic;
using Medora.Model.Institutions;
using Medora.Services.Utils;

namespace Medora.Services.Institutions
{
    public class ServiceBaseInstitution<T, F> : ServiceBase<T, F>, IService<T, F>
        where T : InstitutionDeletedEntity
        where F : IFilter
    {
        protected IUow _uow;
        protected IRepository<T> _repository;
        private IUserService _userService;
        private IFilterConstructor<F, T> _filterConstructor;

        public ServiceBaseInstitution(IUow uow, IUserService userService, IFilterConstructor<F, T> filterConstructor) : base(uow,  userService, filterConstructor)
        {
            _uow = uow;
            _userService = userService;
            _filterConstructor = filterConstructor;
        }

        
        private Expression<Func<T, bool>> GetFilter(F filter, Guid userId)
        {
            var user = _userService.GetById(userId);
            if (user != null && user.InstitutionId.HasValue)
            {
                Expression<Func<T, bool>> startE = o => o.Deleted != true && o.InstitutionId==user.InstitutionId.Value;
                return ConstructFilter(startE, filter);
            }
            return null;
        }

        public int GetCount(Guid userId)
        {
            var user = _userService.GetById(userId);
            if (user != null && user.InstitutionId.HasValue)
            {
                Expression<Func<T, bool>> startE = o => o.Deleted != true && o.InstitutionId == user.InstitutionId.Value;
                return _repository.GetCount(startE);
            }
            return 0;
        }

        
    }
}